<?php
/*
 * Created on May 7, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 		include 'unaccent.php';
		$connect = mysql_connect('localhost','root','123')or die("Error connection:".mysql_error());
		mysql_select_db('hvn', $connect) or die ("Error:".mysql_error());
		
		$sql = "SELECT name, unaccented_name FROM places";// WHERE rtype = 19";
		$result = mysql_query($sql);
		$part = VN_unaccent(strtolower($_GET['part']));
		$length = strlen($part);
		// check the parameter
		if(isset($_GET['part']) and $_GET['part'] != '')
		{	
			$part = VN_unaccent(strtolower($_GET['part']));
	        $parts = explode (' ',$part);
			$results = array();
			$names = array();
			$ids = array();
			
			while($rows=mysql_fetch_array($result))
			{
				$items = array($rows['unaccented_name'] => $rows['name'] );
		 		foreach ($items as $key => $value) 
		 		{
	                $matched = true;
	                $lower_key = strtolower($key);
	                foreach ($parts as $p)
	                {
	                    if (strpos($lower_key, $p) === false) 
	                    {
	                       $matched = false;
	                       break;
	                    }
	                }
	               if ($matched)
		   			{
		   				$id = array($rows['id']);
						$results[] = array('id' => $id, 'name' => $value);
			  		}
	 			}
			}
			echo json_encode ($results);
		}
?>
